//
//  TJOriginView.swift
//  TJSinaWeibo
//
//  Created by 唐后能 on 16/4/14.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SVProgressHUD

class TJOriginView: UIView {

    // 微博数据.
    var status:TJStatus? {
    
        // 给originView的子控件赋值.
        didSet {
            // 头像.
            if let profile_image_url = status?.user?.profile_image_url {
                let url = NSURL(string: profile_image_url)
//                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"), completed: { (image, error, _, _) -> Void in
                    if error != nil {
                    
                        SVProgressHUD.showErrorWithStatus("加载头像出错")
                        return
                    }
                    // 异步绘制图片
                    image.tj_asyncDrawImage(nil,bgColor: UIColor.whiteColor(), isCorner: true, drawFinish: { (image) -> () in
                        self.iconView.image = image
                    })
                })
            }
            // 昵称.
            nameLabel.text = status?.user!.screen_name
            
            // 会员等级.
//            levelView.image = status?.user?.mbrankImage
            if let mbrankImage = status?.user?.mbrankImage {
            
                // 异步绘制会员等级图片
                mbrankImage.tj_asyncDrawImage(nil, bgColor: UIColor.whiteColor(), isCorner: true, drawFinish: { (image) -> () in
                        self.levelView.image = image
                    })
            }else {
                
                self.levelView.image = nil
            }
            
            // 认证图片.
//            verifiedView.image = status?.user?.verifiedImage
            if let verifiedImage = status?.user?.verifiedImage {
            
                
                verifiedImage.tj_asyncDrawImage(nil, bgColor: UIColor.whiteColor(), isCorner: true, drawFinish: { (image) -> () in
                    self.verifiedView.image = image
                })
            }else {
                self.verifiedView.image = nil
            }
            
            // 时间label.-->是发微博的时间,不是用户注册时间.
            timeLabel.text = NSDate.date((status?.created_at) ?? "刚刚").sinaDateDescription()
            
            // 来源.
            sourceLabel.text = status?.source
            
            // 内容.
            contentLabel.attributedText = status?.text?.emoticonNameTextToPicAttText()
            
            // 配图.-->传递给pictureView,让它自己赋值.
            PictureView.picURLS = status?.picURLS
            // 当没有图片的时候,更新配图的顶部约束,把距离内容的间距去掉.-->参照没变,用update.
            PictureView.snp_updateConstraints { (make) -> Void in
             
                let margin = status?.picURLS == nil ? 0 : TJStatusCellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }
            
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI ()
        backgroundColor = UIColor.whiteColor()
    }
    
    
    private func setupUI () {
    
        // 1,添加子控件.
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(levelView)
        addSubview(verifiedView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(PictureView)
        
        // 2,设置约束.
        // 头像.
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(TJStatusCellMargin)
            make.width.height.equalTo(TJStatusCellIconWH)
        }
        // 昵称.
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(TJStatusCellMargin)
        }
        // 会员等级.
        levelView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(TJStatusCellMargin)
            make.width.height.equalTo(15)
        }
        
        // 会员认证.
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.width.height.equalTo(17)
        }
        
        // 时间lebel
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        
        // 来源label
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(TJStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        // 内容label
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(TJStatusCellMargin)
            make.leading.equalTo(self).offset(TJStatusCellMargin)
            make.trailing.equalTo(self).offset(-TJStatusCellMargin)
        }
        
        // 配图.
        PictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(TJStatusCellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(TJStatusCellMargin)
            // 高度由内容决定.
//            make.height.equalTo(100)
            make.size.equalTo(CGSizeZero)
        }
        
        // 给自己设置约束,参照最底部的子控件.
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(PictureView).offset(TJStatusCellMargin)
        }
    }
    
    // MARK - 懒加载子控件.
    // 头像.
    private lazy var iconView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar"))
        // 裁剪成圆形头像.
//        imageView.layer.cornerRadius = 17.5
//        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    // 昵称label.
    private lazy var nameLabel:UILabel = UILabel(fontSize: TJNameLbelTextSize, contentColor: UIColor.darkGrayColor())
    
    // 会员等级图片.
    private lazy var levelView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    
    // 认证图标.
    private lazy var verifiedView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    // 时间label.
    private lazy var timeLabel:UILabel = UILabel(fontSize: TJTimeSize, contentColor: UIColor.orangeColor())
    
    // 来源label.
    private lazy var sourceLabel:UILabel = UILabel(fontSize: TJTimeSize, contentColor: UIColor.lightGrayColor())
    
    // 内容label.
    private lazy var contentLabel:FFLabel = {
    
        let label = FFLabel()
        // 设置.
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(CGFloat(TJStatusContentSize))
        label.textColor = UIColor.grayColor()
        
        //设置FFLabel的代理.
        label.labelDelegate = self
        
        // 设置label宽度,否则行高不对.
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * TJStatusCellMargin
        return label
    }()
    
    // 配图.
    private lazy var PictureView:TJPictureView = TJPictureView()
}

extension TJOriginView : FFLabelDelegate {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        TJPrint("\(text)")
    }
}
